<?php
namespace app\api\controller\xilumarket;

use app\common\controller\Api;
use think\Db;
use think\Exception;
use app\common\model\xilumarket\ProductMessage AS ProductMessageModel;
use think\exception\PDOException;

/**
 * 闲置留言
 * @package app\api\controller\xilumarket
 */
class ProductMessage extends Api {

    protected $noNeedLogin = ['lists'];
    protected $noNeedRight = ['*'];
    /**
     * 发布留言
     */
    public function add_message() {
        $params = $this->request->post('');
        $params['user_id'] = $this->auth->id;
        Db::startTrans();
        try {
            $ret = ProductMessageModel::addMessage($params);
        }catch (Exception|PDOException $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        Db::commit();
        $this->success('留言成功', $ret);

    }

    /**
     * 我发布的留言
     */
    public function my_message() {
        $list = ProductMessageModel::where('product_message.user_id', $this->auth->id)
            ->with([
                'product'=>function($query){$query->withField('id,name,thumb_image');},
//                'childrenMessage'=>function($query){
//                    $query->with(['user'=>function($query){
//                        $query->withField(['id','nickname','bio','avatar','mobile']);
//                    },'puser'=>function($query){
//                        $query->withField(['id','nickname','bio','avatar','mobile']);
//                    }]);
//                }
            ])
            ->order('product_message.id','desc')
            ->paginate();
        $this->success('查询成功',$list);
    }

    /**
     * 留言列表
     * @throws \think\exception\DbException
     */
    public function lists() {
        $product_id = $this->request->param('product_id',null);
        $pagesize = $this->request->param('pagesize',10);
        $user_id = $this->auth->id;
        $list = ProductMessageModel::where('product_message.product_id',$product_id)
            ->with([
                'user'=>function($query){$query->withField('id,avatar,nickname');},
//                'childrenMessage'=>function($query){
//                    $query->with(['user'=>function($query){
//                        $query->withField(['id','nickname','bio','avatar','mobile']);
//                    },'puser'=>function($query){
//                        $query->withField(['id','nickname','bio','avatar','mobile']);
//                    }]);
//                }
            ])
            ->where(function ($query) use($user_id){
                $query->where('product_message.status','2');
                if($user_id){
                    $query->whereOr('user_id',$user_id);
                }
            })
            ->where('product_message.pid',0)
            ->order('product_message.id','desc')
            ->paginate($pagesize)
            ->each(function ($row){
                $row->childlist_count = ProductMessageModel::where('top_id',$row->id)->count(1);
                $row->childComments = [];
                $row->childCommentsMore = ["page"=>1];
            });;
        $this->success('查询成功', $list);
    }

    /**
     * 二级评价
     */
    public function second_lists(){
        $product_id = $this->request->param('product_id',null);
        $product_message_id = $this->request->param('product_message_id',null);
        $pagesize = $this->request->param('pagesize',10);
        $user_id = $this->auth->id;
        $list = ProductMessageModel::field("id,user_id,comment_count,content,createtime")
            ->where('product_message.product_id',$product_id)
            ->with([
                'user'=>function($query){$query->withField('id,avatar,nickname');}
            ])
            ->where(function ($query) use($user_id){
                $query->where('product_message.status','2');
                if($user_id){
                    $query->whereOr('user_id',$user_id);
                }
            })
            ->where('product_message.top_id',$product_message_id)
            ->order('id','desc')
            ->paginate($pagesize);
        $this->success('查询成功', $list);
    }

}